package com.ellecity06.debugtools.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.ellecity06.debugtools.R
import com.ellecity06.debugtools.adapter.ViewPagerAdapter
import com.ellecity06.debugtools.base.BaseDebugActivity
import com.ellecity06.debugtools.databinding.ActivityDebugMainBinding
import com.ellecity06.debugtools.db.DebugToolsDatabase
import com.ellecity06.debugtools.db.dao.RequestDao
import com.ellecity06.debugtools.ui.fragment.error.DebugErrorListFragment
import com.ellecity06.debugtools.ui.fragment.log.DebugLogListFragment
import com.ellecity06.debugtools.ui.fragment.request.DebugRequestListFragment

/**
 * @author ellecity06
 * @time 2023/4/19 16:34
 * @des
 */
internal class DebugMainActivity : BaseDebugActivity<ActivityDebugMainBinding, DebugMainViewModel>(), ViewPager.OnPageChangeListener,
    View.OnClickListener {
    private val requestDao: RequestDao by lazy { DebugToolsDatabase.getDatabase(this).getRequestDao() }
    override val userBaseTitle: Boolean
        get() = false

    companion object {
        fun launch(context: Context) {
            context.startActivity(Intent(context, DebugMainActivity::class.java).apply {
                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            })
        }
    }


    private val fragments: List<Fragment> by lazy { mutableListOf(DebugRequestListFragment(), DebugLogListFragment(), DebugErrorListFragment()) }
    override fun inflateBinding(): ActivityDebugMainBinding {
        return ActivityDebugMainBinding.inflate(layoutInflater)
    }

    override fun initialize(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.debug_tool_name))
        initViewPager()

    }

    override fun initEvent() {
        baseBinding.ivRight.visibility = View.VISIBLE
        baseBinding.ivRight.setOnClickListener(this)
    }

    private fun initViewPager() {
        binding.viewPager.adapter = ViewPagerAdapter(supportFragmentManager, null, fragments)
        binding.viewPager.addOnPageChangeListener(this)
        switchPage(0)
        binding.llRequest.setOnClickListener(this)
        binding.llLog.setOnClickListener(this)
        binding.llError.setOnClickListener(this)
    }

    /**
     * 根据下标选中按钮
     */
    private fun switchPage(position: Int) {
        resetNavView()
        when (position) {
            0 -> {
                binding.tvRequest.isChecked = true
            }
            1 -> {
                binding.tvLog.isChecked = true
            }
            2 -> {
                binding.tvError.isChecked = true
            }
        }
    }

    /**
     * 重置选中按钮
     */
    private fun resetNavView() {
        binding.tvRequest.isChecked = false
        binding.tvLog.isChecked = false
        binding.tvError.isChecked = false
    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

    }

    override fun onPageSelected(position: Int) {

    }

    override fun onPageScrollStateChanged(state: Int) {

    }

    override fun onClick(v: View?) {
        val position: Int
        when (v?.id) {
            R.id.ll_request -> {
                position = 0
                switchPage(position)
                binding.viewPager.currentItem = position
            }
            R.id.ll_log -> {
                position = 1
                switchPage(position)
                binding.viewPager.currentItem = position
            }
            R.id.ll_error -> {
                position = 2
                switchPage(position)
                binding.viewPager.currentItem = position
            }
            else -> {

            }
        }

    }
}